﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Jacobi.Vst.Core.Host;
using Jacobi.Vst.Interop.Host;

namespace Play.VstPluginManagement
{
	public class VstPluginCatalog
	{
		private readonly Func<VstPluginContext, string, VstPluginInfo> _pluginInfoFactory;

		public VstPluginCatalog(
			AppSettings settings, 
			IVstHostCommandStub hostCommandStub,
			Func<VstPluginContext, string, VstPluginInfo> pluginInfoFactory)
		{
			_pluginInfoFactory = pluginInfoFactory;
			
			Plugins = Directory
				.GetFiles(settings.VstPluginPath, "*.dll", SearchOption.AllDirectories)
				.Select(it =>
					{
						try
						{
							return _pluginInfoFactory(
								VstPluginContext.Create(it, hostCommandStub),
								it);
						}
						catch
						{
							return null;
						}
					})
				.Where(it => it != null)
				.ToList();
		}

		public IEnumerable<VstPluginInfo> Plugins { get; private set; }
	}
}
